/*
 * Created by JFormDesigner on Tue May 06 13:29:05 CST 2025
 */

package com.soft.bams.client;

import com.soft.bams.entity.*;
import com.soft.bams.server.ServerTask;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author 21603
 */
public class BusinessPanel extends JPanel {
    ATMClient atmClient;
    private DefaultComboBoxModel<String> model;

    public BusinessPanel(ATMClient atmClient) {
        this.atmClient = atmClient;
        initComponents();
    }

    private void submit(ActionEvent e) {
        try {
            String selectedItem = (String) operationComboBox.getSelectedItem();
            double money = Double.parseDouble(operationTextField.getText());

            Object response = null;

            switch (selectedItem) {
                case "存款":
                    response = atmClient.sendCommand("deposit#" + atmClient.account.getId() + "#" + money);
                    break;
                case "取款":
                    response = atmClient.sendCommand("withdraw#" + atmClient.account.getId() + "#" +
                            atmClient.account.getPassword() + "#" + money);
                    break;
                case "转账":
                    String toAccount = JOptionPane.showInputDialog(this, "请输入对方账户");
                    String pwd = JOptionPane.showInputDialog(this, "请输入密码");
                    response = atmClient.sendCommand("transfer#" + atmClient.account.getId() + "#" +
                            pwd + "#" + toAccount + "#" + money);
                    // 转账成功后立即强制刷新
                    if (response instanceof Account) {
                        atmClient.account = (Account) response;
                        atmClient.forceBalanceRefresh();
                        JOptionPane.showMessageDialog(this,"转账成功");
                    }
                    break;
                case "修改透支额度":
                    response = atmClient.sendCommand("updateCeiling#" + atmClient.account.getId() + "#" +
                            atmClient.account.getPassword() + "#" + money);
                    break;
                case "贷款":
                    response = atmClient.sendCommand("requestLoan#" + atmClient.account.getId() + "#" + money);
                    break;
                case "还贷":
                    response = atmClient.sendCommand("payLoan#" + atmClient.account.getId() + "#" + money);
                    break;
            }

            if (response instanceof Account) {
                atmClient.account = (Account) response;
                initBusiness();
            } else if (response instanceof String) {
                JOptionPane.showMessageDialog(getParent(), response);
            }else {
                JOptionPane.showMessageDialog(getParent(), "操作失败: 未知错误");
            }
        } catch (Exception exception) {
            JOptionPane.showMessageDialog(getParent(), "操作异常: " + exception.getMessage());
        }
    }

    private void back(ActionEvent e) {
        CardLayout cardLayout = (CardLayout) this.getParent().getLayout();
        cardLayout.show(this.getParent(), "main");
        atmClient.removeAccountFromMap();
    }

    public void initBusiness() {
        SwingUtilities.invokeLater(() -> {
            acccountLabel1.setText(String.valueOf(atmClient.account.getId()));
            nameLabel1.setText(atmClient.account.getName());
            balenceLabel1.setText(String.valueOf(atmClient.account.getBalance()));
            ceilingLabel.setVisible(false);
            ceilingLabel1.setVisible(false);
            loanLabel.setVisible(false);
            loanLabel1.setVisible(false);
            model = new DefaultComboBoxModel<>(new String[]{"存款", "取款", "转账"});
            operationComboBox.setModel(model);

            if (atmClient.account instanceof CreditAccount) {
                CreditAccount account = (CreditAccount) atmClient.account;
                ceilingLabel.setVisible(true);
                ceilingLabel1.setVisible(true);
                ceilingLabel1.setText(String.valueOf(account.getCeiling()));
                model.addElement("修改透支额度");
            }
            if (atmClient.account instanceof LoanSavingAccount) {
                LoanSavingAccount account = (LoanSavingAccount) atmClient.account;
                loanLabel.setVisible(true);
                loanLabel1.setVisible(true);
                loanLabel1.setText(String.valueOf(account.getLoanAmount()));
                model.addElement("贷款");
                model.addElement("还贷");
            }
            if (atmClient.account instanceof LoanCreditAccount) {
                LoanCreditAccount account = (LoanCreditAccount) atmClient.account;
                loanLabel.setVisible(true);
                loanLabel1.setVisible(true);
                loanLabel1.setText(String.valueOf(account.getLoanAmount()));
                model.addElement("贷款");
                model.addElement("还贷");
            }
        });
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        accountLabel = new JLabel();
        nameLabel = new JLabel();
        balanceLabel = new JLabel();
        ceilingLabel = new JLabel();
        loanLabel = new JLabel();
        acccountLabel1 = new JLabel();
        nameLabel1 = new JLabel();
        balenceLabel1 = new JLabel();
        ceilingLabel1 = new JLabel();
        loanLabel1 = new JLabel();
        operationComboBox = new JComboBox();
        operationTextField = new JTextField();
        submitButton = new JButton();
        backButton = new JButton();

        //======== this ========
        setLayout(null);

        //---- accountLabel ----
        accountLabel.setText("\u8d26    \u6237\uff1a");
        add(accountLabel);
        accountLabel.setBounds(new Rectangle(new Point(105, 25), accountLabel.getPreferredSize()));

        //---- nameLabel ----
        nameLabel.setText("\u59d3    \u540d\uff1a");
        add(nameLabel);
        nameLabel.setBounds(new Rectangle(new Point(105, 60), nameLabel.getPreferredSize()));

        //---- balanceLabel ----
        balanceLabel.setText("\u4f59    \u989d\uff1a");
        add(balanceLabel);
        balanceLabel.setBounds(new Rectangle(new Point(105, 95), balanceLabel.getPreferredSize()));

        //---- ceilingLabel ----
        ceilingLabel.setText("\u4fe1\u7528\u989d\u5ea6\uff1a");
        add(ceilingLabel);
        ceilingLabel.setBounds(new Rectangle(new Point(95, 130), ceilingLabel.getPreferredSize()));

        //---- loanLabel ----
        loanLabel.setText("\u8d37 \u6b3e \u989d\uff1a");
        add(loanLabel);
        loanLabel.setBounds(new Rectangle(new Point(100, 160), loanLabel.getPreferredSize()));

        //---- acccountLabel1 ----
        acccountLabel1.setText(" ");
        add(acccountLabel1);
        acccountLabel1.setBounds(235, 25, 150, acccountLabel1.getPreferredSize().height);

        //---- nameLabel1 ----
        nameLabel1.setText(" ");
        add(nameLabel1);
        nameLabel1.setBounds(235, 60, 150, nameLabel1.getPreferredSize().height);

        //---- balenceLabel1 ----
        balenceLabel1.setText(" ");
        add(balenceLabel1);
        balenceLabel1.setBounds(235, 95, 150, balenceLabel1.getPreferredSize().height);

        //---- ceilingLabel1 ----
        ceilingLabel1.setText(" ");
        add(ceilingLabel1);
        ceilingLabel1.setBounds(235, 130, 150, ceilingLabel1.getPreferredSize().height);

        //---- loanLabel1 ----
        loanLabel1.setText(" ");
        add(loanLabel1);
        loanLabel1.setBounds(235, 160, 150, loanLabel1.getPreferredSize().height);
        add(operationComboBox);
        operationComboBox.setBounds(65, 210, 120, operationComboBox.getPreferredSize().height);
        add(operationTextField);
        operationTextField.setBounds(200, 210, 100, operationTextField.getPreferredSize().height);

        //---- submitButton ----
        submitButton.setText("\u63d0\u4ea4");
        submitButton.addActionListener(e -> submit(e));
        add(submitButton);
        submitButton.setBounds(new Rectangle(new Point(100, 265), submitButton.getPreferredSize()));

        //---- backButton ----
        backButton.setText("\u8fd4\u56de");
        backButton.addActionListener(e -> back(e));
        add(backButton);
        backButton.setBounds(new Rectangle(new Point(205, 265), backButton.getPreferredSize()));

        setPreferredSize(new Dimension(400, 330));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel accountLabel;
    private JLabel nameLabel;
    private JLabel balanceLabel;
    private JLabel ceilingLabel;
    private JLabel loanLabel;
    private JLabel acccountLabel1;
    private JLabel nameLabel1;
    private JLabel balenceLabel1;
    private JLabel ceilingLabel1;
    private JLabel loanLabel1;
    private JComboBox operationComboBox;
    private JTextField operationTextField;
    private JButton submitButton;
    private JButton backButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
